package com.sample.playlist;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;

import com.sample.blank.R;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener, OnCompletionListener
{
	Button b1;		//Add song
	Button b2;		//Play song
	Button b3;		//Pause song
	Button b4;		//Forward
	Button b5;		//Back
	Button b6;		//Remove song
	TextView t1;		//Current song
	static ArrayList<String> playlist = new ArrayList<String>();
	static ArrayList<String> playlistPaths = new ArrayList<String>();
	static int currentIndex = 0;
	static String tempPath = "";
	static MediaPlayer player = new MediaPlayer();
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button4);
        b3 = (Button) findViewById(R.id.button5);
        b4 = (Button) findViewById(R.id.button6);
        b5 = (Button) findViewById(R.id.button7);
        b6 = (Button) findViewById(R.id.button2);
        
        t1 = (TextView) findViewById(R.id.textView1);
        
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);
        b5.setOnClickListener(this);
        b6.setOnClickListener(this);
    }
    public void onResume()
    {
    	super.onResume();
        //if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
       //     processIntent(getIntent());
        //}
    	while(currentIndex >= playlist.size())
    		currentIndex--;
    	while(currentIndex < 0)
    		currentIndex++;
    	try{
    		t1.setText(playlist.get(currentIndex));
    	}catch(Exception e){}
    	try
    	{
    		Bundle bundle = getIntent().getExtras();
    		playlistPaths.add(bundle.getString("path"));
    		playlist.add(bundle.getString("name"));
    		//t1.setText("Current Song:\n" + selectedFileName);
    	}catch(Exception e){}
    }
	public void onClick(View v) 
	{
		if(v.equals(b1))
		{
			Intent intent = new Intent(this, AddSong.class);
			this.startActivity(intent);
		}
		if(v.equals(b2))
		{
			if(tempPath != playlistPaths.get(currentIndex))
			{
				tempPath = playlistPaths.get(currentIndex);
				player.stop();
				player.reset();
				player = new MediaPlayer();
				player.setLooping(false);
				try 
				{
					FileInputStream fis = new FileInputStream(playlistPaths.get(currentIndex));
					player.reset();
					player.setDataSource(fis.getFD());				
					player.prepare();
					player.start();
					t1.setText(playlist.get(currentIndex));
					onClick(b2);
				} 
				catch (Exception e){}
			}
			else
			{
				player.start();
			}
		}
		if(v.equals(b3))
		{
			player.pause();
		}
		if(v.equals(b4))
		{
			currentIndex++;
			try{
				onClick(b2);
			}catch(Exception e){Toast.makeText(this, "Add more songs!", Toast.LENGTH_LONG).show();}
		}
		if(v.equals(b5))
		{
			currentIndex--;
			try{
				onClick(b2);
			}
			catch(Exception e){Toast.makeText(this, "Add more songs!", Toast.LENGTH_LONG).show();}
		}
		if(v.equals(b6))
		{
			if(playlist.size() > 0)
			{
				Intent intent = new Intent(this, RemoveSong.class);
				intent.putExtra("songs", playlist);
				this.startActivity(intent);
				Bundle bundle = getIntent().getExtras();
				playlist.remove(bundle.getInt("index"));
				playlistPaths.remove(bundle.getInt("index"));
			}
			else
				Toast.makeText(this, "Add more songs!", Toast.LENGTH_LONG).show();
		}
	}
	public void onCompletion(MediaPlayer mp) 
	{
		try{
			mp.stop();
			mp.release();
			if(currentIndex + 1 != playlist.size())
			{
				currentIndex++;
				onClick(b2);
			}
		}catch(Exception e){}
	}
	public void onPause()
	{
		super.onPause();
		//try{
		//	player.stop();
		//	player.release();
		//}catch(Exception e){}
	}
	public void onStop()
	{
		super.onStop();
		//try{
		//	player.stop();
		//	player.release();
		//}catch(Exception e){}
	}
//	public NdefMessage createNdefMessage(NfcEvent event) {
//        //String text = input.getText().toString();
//        File file = new File(selectedFilePath);
//
//        byte[] b = new byte[(int) file.length()];
//        try {
//        	FileInputStream fileInputStream = new FileInputStream(file);
//        	fileInputStream.read(b);
//        }catch(Exception e){}
//        NdefMessage msg = new NdefMessage(new NdefRecord[] { 
//        		createMimeRecord("application/com.sample.nfctest", b)});
//        return msg;
//	}
//    @Override
//    public void onNewIntent(Intent intent) {
//        // onResume gets called after this to handle the intent
//        setIntent(intent);
//    }
//    void processIntent(Intent intent) 
//    {
//        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
//                NfcAdapter.EXTRA_NDEF_MESSAGES);
//        // only one message sent during the beam
//        NdefMessage msg = (NdefMessage) rawMsgs[0];
//        // record 0 contains the MIME type, record 1 is the AAR, if present
//        try{
//	        tempMp3 = File.createTempFile("mySong", "mp3", getCacheDir());
//	        tempMp3.deleteOnExit();
//	        FileOutputStream fos = new FileOutputStream(tempMp3);
//	        fos.write(msg.getRecords()[0].getPayload());
//	        fos.close();
//        }catch(Exception e){}
//    }
    /*public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
        byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
        NdefRecord mimeRecord = new NdefRecord(
                NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
        return mimeRecord;
    }*/
}